feat(http/unstable): add radix tree router; keep linear scan as routeLinear#7075
Open
esroyo wants to merge 4 commits intodenoland:mainfrom
Open
feat(http/unstable): add radix tree router; keep linear scan as routeLinear#7075esroyo wants to merge 4 commits intodenoland:mainfrom
esroyo wants to merge 4 commits intodenoland:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7075 +/- ##
==========================================
- Coverage 94.61% 94.60% -0.01%
==========================================
Files 634 634
Lines 51799 51942 +143
Branches 9329 9376 +47
==========================================
+ Hits 49009 49141 +132
- Misses 2216 2223 +7
- Partials 574 578 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
786873d to
d522ffa
Compare
bartlomieju
reviewed
Apr 9, 2026
…Linear Add routeRadix(), a radix tree router that provides O(segments) dispatch for static, parametric, and wildcard routes. Routes with complex URLPattern syntax (regex constraints, optional groups, inline wildcards, modifier suffixes) fall back to linear matching while preserving insertion order. - routeRadix: radix tree with fallback to linear for complex patterns - routeLinear: the original linear scan, extracted as its own export - route: re-exported alias for routeRadix (backward compatible) The radix router matches routeLinear semantics exactly — insertion order is always respected, even when static and parametric routes overlap at the same tree depth. Benchmarks show 1.5–9x improvement on static/parametric/wildcard routes, with negligible overhead on complex fallback patterns.
Replace the per-request `radixCandidates` array + `Array#sort` + merge with a single-slot match using `minIndex` pruning and inline pathname iteration. No candidate array, segments array, or sort closure on the hot path. Each `RouteNode` now tracks `minIndex` (lowest insertion index in the subtree) and `hasStaticChildren` (skip the substring slice when there are no static children). Fallback routes registered before the lowest radix index are scanned first so their hits prune the tree walk.
…allback URLPattern requires reserved characters (`+`, `?`, `*`) to be backslash-escaped to match literally. The escape is preserved in `pattern.pathname` (e.g. `/c\+\+`), so the radix tree was inserting such patterns as static keys that could never match the unescaped request path (`/c++`), making those routes unreachable. Treat any segment containing a backslash as complex so URLPattern remains the authoritative matcher via the linear fallback. Adds a regression test covering both single (`\+`) and consecutive (`\+\+`) escape forms.
The radix router is brand new; keep `route` bound to the better-tested `routeLinear` while it matures. Users who want the radix optimizations opt in via the `routeRadix` named export.
8b90273 to
632ecdc
Compare
Author
|
@bartlomieju Thank you for the insightful comments! I mostly agree with your feedback (except the Array#sort comment perhaps) and have addressed all the points you raised. Thanks so much for your time ❤️ |
Author
|
[updated] ---
config:
themeVariables:
xyChart:
plotColorPalette: "#e05c5c, #4a90d9"
---
xychart-beta
title "AMD Ryzen 7 PRO 6850U — route benchmark"
x-axis [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y-axis "Avg latency (µs)" 0 --> 6
line [0.92, 1.96, 5.86, 1.63, 5.6, 1.53, 2.72, 1.06, 3.17, 0.93, 1.46, 1.89]
line [1, 1.12, 1.07, 0.43, 0.43, 1.18, 1.31, 0.54, 1.39, 1.05, 1.44, 1.88]
🔴
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
routeRadix(), a radix tree router that provides O(segments) dispatch for static, parametric, and wildcard routes. Routes with complex URLPattern syntax (regex constraints, optional groups, inline wildcards, modifier suffixes) fall back to linear matching while preserving insertion order.routeRadix: radix tree with fallback to linear for complex patternsrouteLinear: the original linear scan, extracted as its own exportroute: re-exported alias forrouteLinearThe radix router matches routeLinear semantics exactly; insertion order is always respected, even when static and parametric routes overlap at the same tree depth.
Benchmarks show 1.5–9x improvement on static/parametric/wildcard routes, with negligible overhead on complex fallback patterns.
--- config: themeVariables: xyChart: plotColorPalette: "#e05c5c, #4a90d9" --- xychart-beta title "route: 🔴 linear vs 🔵 radix - Avg latency (µs)" x-axis [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y-axis "Avg latency (µs)" 0 --> 7 line [0.91, 2.04, 6.1, 1.63, 5.54, 1.49, 2.84, 1.08, 3.15, 0.9, 1.45, 1.98] line [1.08, 1.38, 1.39, 0.62, 0.61, 1.55, 1.66, 0.91, 1.73, 1.46, 1.62, 2.1]